<?php

namespace app\admin\controller;

use think\Controller;
use app\common\model\IconTypes as Mtype;

class Icontypes extends Controller
{

    /**
     * 图标类型页
     */
    public function types()
    {
        return $this->fetch();
    }


    public function simple_machine_types_json_tree()
    {
        return js_return(1, 'success', tree_machine_types());
    }


    public function get_machines()
    {
        $m     = new M();
        $types = $m->getMachines();
        $count = $m->getMachinesCount();
        $lists = ['code' => 0, 'msg' => '', 'count' => $count, 'data' => []];

        foreach ($types as $key => $val) {
            $lists['data'][] = $val;
        }
        return $lists;
    }


    /**
     * 获取josn数据
     *
     */
    public function get_json()
    {
        $m     = new Mtype();
        $count = count($m->getAllTypes());
        $types = $m->gets();
        $lists = ['code' => 0, 'msg' => '', 'data' => [],'count'=>$count];
        $modes = Mtype::getMode();

        foreach ($types as $key => $val) {

            $depth              = substr_count($val['path'], '-');
            $val['name']        = str_repeat("&nbsp;|--", $depth) . $val['name'];
            $val['mode_name']   = $modes[$val['mode']];
            $val['color_block'] = '<span style="display:inline-block;width:100%;height:100%;background:' . $val['color'] . ';"></span>';
            $val['pic_img']     = '<img src="' . $val['pic'] . '" width="40" height="40" />';
            $lists['data'][]    = $val;
        }
        return $lists;
    }


    /**
     * 类型添加
     */
    public function types_add()
    {
        $mp        = new \app\common\model\PicLists();
        $first_pic = $mp->find()->pic;
        $pics      = $mp->getAll();
        $modes     = Mtype::getMode();
        return $this->fetch('', compact('modes', 'first_pic', 'pics'));
    }


    /**
     * 类型编辑
     */
    public function types_edit()
    {
        $mp        = new \app\common\model\PicLists();
        $first_pic = $mp->find()->pic;
        $pics      = $mp->getAll();
        $m         = new Mtype();
        $modes     = Mtype::getMode();
        $id        = input('id');
        $s         = $m->getById($id);
        return $this->fetch('', compact('id', 's', 'modes', 'first_pic', 'pics'));
    }


    /**
     * 添加提交
     */
    public function contents_submit()
    {
        $m = new Mtype();


        if (input('id')) {
            $s = $m->modify(input('post.'), input('id'));
        }
        else {
            $s = $m->add(input('post.'));
        }
        return js_return($s[0], $s[1]);
    }


    /**
     * 删除
     */
    public function del_submit()
    {
        $m  = new Mtype();
        $rs = $m->remove(input('id'));
        return js_return($rs[0], $rs[1]);
    }

}
